<?php
/**
 * Created by PhpStorm.
 * User: wanghaibo
 * Date: 17/11/27
 * Time: 17:10
 */

namespace App\Http\Controllers\Admin\Menu;


use App\Http\Controllers\Controller;
use App\Service\Menu;
use App\Repositories\Menu as MenuRepository;
use App\Service\Permission;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    private $menuManager;
    private $menuRepository;
    private $request;
    public function __construct (Menu $menuManager,MenuRepository $menuRepository)
    {
        $this->menuManager = $menuManager;
        $this->menuRepository = $menuRepository;
    }

    public function index(Request $request){
        $this->request = $request;
        if(isset($this->request->act)){
            $act = $this->request->act;
            return $this->{$act}();
        }else{
            return view('admin.menu.menu.index');
        }
    }

    /**
     * ajax列表
     * @return array
     */
    public function indexAjax(Request $request){
        return $this->page_json_render($this->menuManager->getPageList($request));
    }

    /**
     * 编辑页面
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id){
        $model = $this->menuRepository->find($id);
        $permission = Permission::getByGuard_Name('web');
        $permission = $permission ? $permission->toArray() : [];
        $topAry = $this->menuManager->getTopMenus();
        $topAry = $topAry?$topAry->toArray():[];
        return view('admin.menu.menu.add-edit',[
            'model' =>  $model,
            'permission'    =>  $permission,
            'topAry'    =>  $topAry,
            'id'    =>  $id
        ]);
    }


    /**
     * 添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(){
        $permission = Permission::getByGuard_Name('web');
        $permission = $permission ? $permission->toArray() : [];
        $topAry = $this->menuManager->getTopMenus();
        $topAry = $topAry?$topAry->toArray():[];
        return view('admin.menu.menu.add-edit',[
            'permission'    =>  $permission,
            'topAry'    =>  $topAry
        ]);
    }

    /**
     * 保存或插入
     * @return \Illuminate\Http\RedirectResponse
     */
    public function saveOrInsert(Request $request){
        //dd($this->request->post());
        if($request->parent_id==0){
            $this->validate($request,[
                'title'    =>  'required|max:50',
                'permission'    =>  'required',
                'parent_id' =>  'required',
            ]);
        }else{
            $this->validate($request,[
                'title'    =>  'required|max:50',
                'permission'    =>  'required',
                'parent_id' =>  'required',
                'href'   =>  'required',
            ]);
        }

        $this->menuManager->insertOrUpdate($request->post());
        return redirect()->route('admin.dev-menu')->with(
            $this->success()
        );
    }

    /**
     * @return array
     */
    private function delete(){
        try{
            $this->menuManager->del($this->request->id);
            return  ['info'  =>  'success'];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }

    private function sortCol(){
        try{
            $this->menuManager->sortCol($this->request->id,$this->request->sort);
            return  ['info'  =>  'success'];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }
}